const { ObjectId } = require("mongodb");
const { collections } = require("../db/mongo_conf");
const mongoUtil = require("../db/mongo_util");

const collection = collections.goods;

function addGoods(goods) {
    return mongoUtil.insertOne(collection, goods);
}

function deleteGoods(id) {
    const whereOption = { _id: ObjectId(id) };
    return mongoUtil.deleteOne(collection, whereOption);
}

function updateGoods(id, goods) {
    const whereOption = { _id: ObjectId(id) };
    return mongoUtil.updateOne(collection, whereOption, goods);
}

// getGoods({name:"paper"}, { skip:10, limit:5 })
function getGoods(whereOption, { skip, limit }) {
    return mongoUtil.find(collection, whereOption, { skip, limit });
}

module.exports = {
    addGoods,
    deleteGoods,
    updateGoods,
    getGoods,
};
